<template>
  <div class="app-container">
    <el-form ref="info" :model="info" label-width="120px">
      <el-form-item label="用户编号" style="display:none;" prop="id">
        <el-input v-model="info.id" />
      </el-form-item>
      <el-row>
        <el-col :span="16">
          <el-row>
            <el-col>
              <el-form-item label="用户名：" prop="username">
                <el-input v-model="info.username" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="电话号码：" prop="tel">
                <el-input v-model="info.tel" />
              </el-form-item>
            </el-col>
            <el-col :span="11" :offset="2">
              <el-form-item label="邮箱：" prop="email">
                <el-input v-model="info.email" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="4" :offset="1">
          <el-upload
            ref="upload"
            action="#"
            class="avatar-uploader"
            :on-success="handleAvatarSuccess"
            :on-change="handleChange"
            :http-request="uploadFile"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload">
            <img :src="imgSrc" class="avatar">
            <!-- <i v-else class="el-icon-plus avatar-uploader-icon"></i> -->
          </el-upload>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :offset="6">
          <el-form-item >
            <el-button type="primary" @click="onSubmit">保存</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { updateInfo, getInfo } from '@/api/user'
import { fileUpload } from '@/api/file'
import router from '@/router'
export default {
  data() {
    return {
      info: {
        id: 0,
        username: '',
        tel: '',
        email: '',
        avatar: '',
      },
      imgSrc: '',
    }
  },
  methods: {
    async onSubmit() {
      const response = await updateInfo(this.info)
      const user = response.data
      this.info = user
      if (user != null) {
        this.$message({
          message: response.message,
          type: 'success'
        })
        this.$store.commit('user/SET_NAME', response.data.username)
        this.$store.commit('user/SET_AVATAR', response.data.avatar)
      }
    },
    fetchData() {
      getInfo().then(response => {
        const user = response.data
        this.info = user
        this.imgSrc = this.$store.getters.avatar;
      })
    },
    async uploadFile(param) {
      this.fd = new FormData()
      this.fd.append('file', param.file)
      const {data} = await fileUpload(this.fd)
      this.info.avatar = data
    },
    handleChange(file) {
      this.imgSrc = URL.createObjectURL(file.raw);
    },
    handleAvatarSuccess(res, file) {
      this.imgSrc = this.$store.getters.avatar
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

<style>
.line{
  text-align: center;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

